package com.fh.fruit.util;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;


public class CustomUtil {

	//生成固定长度随机数
	public static int buildRandom(int length) {
		int num = 1;
		double random = Math.random();
		if (random < 0.1) {
			random = random + 0.1;
		}
		for (int i = 0; i < length; i++) {
			num = num * 10;
		}
		return (int) ((random * num));
	}
	
	public static String codeSuf(int length, int id){
		
		DecimalFormat dfHead = new DecimalFormat("0000");
		String formatStr = dfHead.format(id);
		String tempStr = StringUtils.substring(formatStr, formatStr.length()-4);
		String str = buildRandom(length)+tempStr;
		return str;
	}



	
	public static void sleep(int minTime, int maxTime){
		try {
			Random rand = new Random();
			int sleepTime = rand.nextInt(maxTime
					- minTime)
					+ minTime;
			Thread.sleep(sleepTime);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	
	

	public static String mapToJson(Map<String, Object> mp){
		
		ObjectMapper om = new ObjectMapper();
		
		 String json ="";
			try {
				json = om.writeValueAsString(mp);
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		return json;	
	}
	
	/**
	 * 元转换成分
	 * 
	 * @param money
	 * @return
	 */
	public static String getMoney(BigDecimal currency) {
		if (currency == null) {
			return "";
		}
		BigDecimal res = currency.multiply(new BigDecimal(100)).setScale(0,
				BigDecimal.ROUND_DOWN);

		return res.toString();
	}
	
	
	
	public static void doBgReq(HttpServletResponse response,
			String postUrl,String method, Map<String, ?> paramMap) {
		PrintWriter out = null;
		StringBuffer sb = new StringBuffer();
		try {
			response.setContentType("text/html;charset=utf-8");
			sb.append("<form name=\"postSubmit\" method=\"").append(method).append("\" action=\"").append(postUrl).append("\" >\n");
			
			for (String key : paramMap.keySet()) {
				sb.append("<input type=\"hidden\" name=\"").append(key).append("\" value=\"").append(paramMap.get(key)).append("\">\n");
			}
			sb.append("</form>\n<script>  document.postSubmit.submit()</script>");
			out = response.getWriter();
			out.println(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	public static void doBgReq(HttpServletResponse response,
			String content) {
		PrintWriter out = null;
		try {
			response.setContentType("text/html;charset=utf-8");
			out = response.getWriter();
			out.println(content);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

		

	/**
	 * 拼接字符串
	 * @param objs
	 * @return
	 */
	public static String strAppend(Object ... objs ){
		StringBuffer sb = new StringBuffer();
		for(Object obj:objs){
			sb.append(obj);
		}
		return sb.toString();
		
	}
	
	public static String subPhone(String phone){
		return phone.substring(0, 3) + "****"
				+ phone.substring(phone.length() - 4, phone.length());
	}

}
